﻿using System;
using GMap.NET;
using GMap.NET.MapProviders;
using NLog;

namespace ShkSimulation.desktop.component.tools.map.AMap {

	public class AMapProvider : AMapProviderBase {

		private static readonly Logger log = LogManager.GetLogger(nameof(AMapProvider));

		public delegate void QueryProgressDelegate(long completedCount, long total);

		public static readonly AMapProvider Instance;

		private readonly Guid id = new Guid("EF3DD303-3F74-4938-BF40-232D0595EE88");

		public override Guid Id {
			get { return id; }
		}

		private readonly string cnName = "高德地图";

		public string CnName {
			get { return this.cnName; }
		}

		private readonly string name = "AMap";

		public override string Name {
			get { return name; }
		}

		static AMapProvider() {
			Instance = new AMapProvider();
			GMapProviders.List.Add(Instance);
		}

		public override PureImage GetTileImage(GPoint pos, int zoom) {
			try {
				string url = MakeTileImageUrl(pos, zoom, LanguageStr);
				return GetTileImageUsingHttp(url);
			} catch (Exception) {
				return null;
			}
		}

		string MakeTileImageUrl(GPoint pos, int zoom, string language) {
			var num = (pos.X + pos.Y) % 4 + 1;
			string url = string.Format(UrlFormat, num, pos.X, pos.Y, zoom);
			return url;
		}

		//http://webrd01.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=1&style=7&x=3399&y=1664&z=12
		static readonly string UrlFormat =
			"http://webrd0{0}.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=1&style=8&x={1}&y={2}&z={3}";

	}

}